package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.dto.projectInfo.ProjectLogDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.ProjectLogRepository;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.service.ProjectLogService;
import com.dg.cuckoo.admin.vo.PageDto;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProjectLogServiceimpl implements ProjectLogService {

    @Autowired
    private ProjectLogRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;

    @Override
    public void save(ProjectLogDto dto, MultipartFile[] files,String projectId) throws Exception {
        powerfulSave(dto,files,projectId);
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        System.err.println("id:"+id);
        repository.deleteById(id);
        List<FileDto> dtos = fileRepository.findByByMark(id);
        fileRepository.deleteAll(dtos);
    }

    @Override
    public String openSerialInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        ProjectLogDto dto = repository.findById(id).get();
        List<FileDto> files = fileRepository.findByByMark(id);
        List<String> urls=new ArrayList<>();
        for(FileDto f:files){
            urls.add(f.getFileUrl());
        }
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("urls",urls);
        return json.toString();
    }

    @Override
    public String openTextNumberInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        return repository.findById(id).get().getContentBox();
    }

    @Transactional
    @Override
    public void updata(String id, String projectId, ProjectLogDto dto, MultipartFile[] files) throws Exception {
        powerfulUpdate(1,id,projectId,dto,files);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize,String projectId) {
        return powerfulPage(page,pageSize,projectId);
    }

    @Override
    public void powerfulSave(ProjectLogDto dto, MultipartFile[] files, String projectId) throws Exception {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        savecheck(dto);
        dto.setProjectId(projectId);
        ProjectLogDto save = repository.save(dto);
        if(files!=null){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
    }

    public PageModel powerfulPage( Integer page, Integer pageSize,String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        Integer one = page;
        page = (page - 1) * pageSize;
        List<PageDto> pageDtos=new ArrayList<>();
        List<ProjectLogDto> all = repository.findByProjectId(projectId);
        for (ProjectLogDto p:all){
            List<FileDto> files = fileRepository.findByByMark(p.getId());
            List<String> newFiles=new ArrayList<>();
            for (FileDto f:files){
                newFiles.add(f.getFileUrl());
            }
            pageDtos.add(new PageDto(newFiles,p));
        }
        List<PageDto> data = pageDtos.stream().skip(page).limit(pageSize).collect(Collectors.toList());
        return new PageModel(one,pageSize,all.size(),data);
    }

    @Override
    public void powerfulUpdate(Integer type, String id, String projectId, ProjectLogDto dto, MultipartFile[] files) throws Exception {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        dto.setType(type);
        savecheck(dto);
        dto.setProjectId(projectId);
        ProjectLogDto save = repository.save(dto);
        if (files!=null){
        if(files.length>0){
            fileRepository.deleteAll(fileRepository.findByByMark(id));
            projectInfoServer.powerfulFileSave(files,save.getId());
        }
        }
    }


    public void savecheck(ProjectLogDto dto){

    }
}
